package com.tofine.smarthome.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.activeandroid.query.Select;
import com.tofine.smarthome.R;
import com.tofine.smarthome.adapter.DeviceChooseAdapter;
import com.tofine.smarthome.base.BaseActivity;
import com.tofine.smarthome.model.Device;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * @author ddMax
 * @since 2016/5/31 0:13
 * 说明：设备选择Activity
 */
public class SceneAddChooseDeviceActivity extends BaseActivity {

    @Bind(R.id.device_list) RecyclerView mDeviceList;
    @Bind(R.id.btn_confirm) AppCompatButton mBtnConfirm;
    private DeviceChooseAdapter mAdapter;
    private List<Device> mDeviceData;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_add_choose_device;
    }

    @Override
    protected int getActionBarCustomView() {
        return R.layout.actionbar_info;
    }

    @Override
    protected int getActionBarTitle() {
        return R.string.title_activity_scene_add_choose_device;
    }

    @Override
    protected boolean hasBackButton() {
        return true;
    }

    @Override
    public void initView() {
        mDeviceData = new Select().from(Device.class).execute();
        mAdapter = new DeviceChooseAdapter(this, mDeviceData);
        mDeviceList.setLayoutManager(new LinearLayoutManager(this));
        mDeviceList.setAdapter(mAdapter);
    }

    @Override
    public void initData() {

    }

    /**
     * 确定按钮点击事件
     */
    @OnClick(R.id.btn_confirm)
    public void onBtnConfirmClick() {
        ArrayList<Device> selectedDeivces = mAdapter.getSelectedDevices();
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("selected", selectedDeivces);
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    }
}
